package com.fengze.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fengze.business.vo.TreeSelect;
import com.fengze.common.annotation.AutoSetInsertValue;
import com.fengze.common.annotation.AutoSetUpdateValue;
import com.fengze.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fengze.business.mapper.FzProductsCategoryMapper;
import com.fengze.business.domain.FzProductsCategory;
import com.fengze.business.service.IFzProductsCategoryService;

/**
 * 产品类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-26
 */
@Service
public class FzProductsCategoryServiceImpl implements IFzProductsCategoryService 
{
    @Autowired
    private FzProductsCategoryMapper fzProductsCategoryMapper;

    /**
     * 查询产品类别
     * 
     * @param id 产品类别主键
     * @return 产品类别
     */
    @Override
    public FzProductsCategory selectFzProductsCategoryById(Long id)
    {
        return fzProductsCategoryMapper.selectFzProductsCategoryById(id);
    }

    /**
     * 查询产品类别列表
     * 
     * @param fzProductsCategory 产品类别
     * @return 产品类别
     */
    @Override
    public List<FzProductsCategory> selectFzProductsCategoryList(FzProductsCategory fzProductsCategory)
    {
        return fzProductsCategoryMapper.selectFzProductsCategoryList(fzProductsCategory);
    }

    /**
     * 新增产品类别
     * 
     * @param fzProductsCategory 产品类别
     * @return 结果
     */
    @AutoSetInsertValue
    @Override
    public int insertFzProductsCategory(FzProductsCategory fzProductsCategory)
    {
        Long pid = fzProductsCategory.getPid();
        if(pid == null){
            fzProductsCategory.setPid(0L);
        }

        fzProductsCategory.setCreateTime(DateUtils.getNowDate());
        return fzProductsCategoryMapper.insertFzProductsCategory(fzProductsCategory);
    }

    /**
     * 修改产品类别
     * 
     * @param fzProductsCategory 产品类别
     * @return 结果
     */
    @AutoSetUpdateValue
    @Override
    public int updateFzProductsCategory(FzProductsCategory fzProductsCategory)
    {
        fzProductsCategory.setUpdateTime(DateUtils.getNowDate());
        return fzProductsCategoryMapper.updateFzProductsCategory(fzProductsCategory);
    }

    /**
     * 批量删除产品类别
     * 
     * @param ids 需要删除的产品类别主键
     * @return 结果
     */
    @Override
    public int deleteFzProductsCategoryByIds(Long[] ids)
    {

        // 如果有子类使用则不能删除
        int useCount = fzProductsCategoryMapper.queryUseCountByIds(ids);
        if(useCount> 0){
            // 有其他类引用不可以删除
            return -1;
        }


        return fzProductsCategoryMapper.deleteFzProductsCategoryByIds(ids);
    }

    /**
     * 删除产品类别信息
     * 
     * @param id 产品类别主键
     * @return 结果
     */
    @Override
    public int deleteFzProductsCategoryById(Long id)
    {
        return fzProductsCategoryMapper.deleteFzProductsCategoryById(id);
    }

    @Override
    public List<TreeSelect> buildTreeSelect(List<FzProductsCategory> categorys) {
        List<FzProductsCategory> trees = buildTree(categorys);
        return trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private List<FzProductsCategory> buildTree(List<FzProductsCategory> categorys) {
        // 获取顶层类
        List<FzProductsCategory> topList = categorys.stream().filter(cg -> cg.getPid().longValue() == 0).sorted((a,b) -> a.getSeq().compareTo(b.getSeq())).collect(Collectors.toList());
        for (FzProductsCategory fzProductsCategory : topList) {
            recursionFn(categorys,fzProductsCategory);
        }
        return topList;
    }

    @Override
    public List<FzProductsCategory> selectCategoryList(FzProductsCategory category) {
        return fzProductsCategoryMapper.selectAll(category);
    }

    /**
     * 递归列表
     * @param list 所有数据
     * @param category 某一数据
     */
    private void recursionFn(List<FzProductsCategory> list, FzProductsCategory category){
        // 获取子节点列表
        List<FzProductsCategory> childList = getChildList(list,category);
        category.setChildren(childList);

        for (FzProductsCategory fzProductsCategory : childList) {
            if(hasChild(list,fzProductsCategory)){
                recursionFn(list, category);
            }
        }
    }

    /**
     * 判断是否有子数据
     *
     */
    private boolean hasChild(List<FzProductsCategory> list, FzProductsCategory category){
        return list.stream().anyMatch(cg -> cg.getPid().longValue() == category.getId().longValue());
    }

    /**
     * 获取所有子数据，以seq升序排列
     * @param list  数据集合
     * @param category 某一数据
     * @return
     */
    private List<FzProductsCategory> getChildList(List<FzProductsCategory> list, FzProductsCategory category){
        List<FzProductsCategory> tmpList = new ArrayList<>();
        List<FzProductsCategory> collect = list.stream().filter(cg -> cg.getPid().longValue() == category.getId().longValue()).sorted((a, b) -> a.getSeq().compareTo(b.getSeq())).collect(Collectors.toList());
        return collect;
    }
}
